package 蚂蚁课堂;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        /**
         * 初始化计数器为2
         */

        final CountDownLatch countDownLatch = new CountDownLatch(2);
        Thread t1=new Thread(new Runnable(){

            public void run() {
                System.out.println("t1开始..............");
                try {
                    //模拟查询数据库的延时
                    Thread.sleep(5000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("t1结束..............");
                /**
                 * t1执行完成之后将计数器递减
                 */
                countDownLatch.countDown();
            }
        });

        Thread t2=new Thread(new Runnable(){

            public void run() {
                System.out.println("t2开始..............");
                try {
                    //模拟查询数据库的延时
                    Thread.sleep(10000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("t2结束..............");
                /**
                 * t2执行完成之后将计数器递减
                 */
                countDownLatch.countDown();
            }
        });
        t1.start();
        t2.start();
        boolean await = countDownLatch.await(2000L, TimeUnit.SECONDS);
        System.out.println(await);
        System.out.println("主线程开始执行任务..............");
    }
}
